#include"comm.h"
//服务端
//1.先清空权限掩码，然后创建管道 注意指定权限
//2.以读取方式打开管道
//3.以写入方式打开一个拷贝文件
//4.从管道当中读取数据到这个拷贝文件当中
//最后要关闭这两个文件描述符
int main()
{
    umask(0);
    if(mkfifo(MY_FIFO,0666) < 0)
    {
        perror("mkfifo:");
        exit(1);
    }
    int fd = open(MY_FIFO,O_RDONLY);
    if(fd<0)
    {
        perror("open1:");
        exit(2);
    }
    int copy_file = open("./file_copy.txt",O_WRONLY | O_CREAT | O_TRUNC,0666);
    if(copy_file<0)
    {
        perror("open2:");
        exit(3);
    }
    while(1)
    {
        char buff[128] = {0};
        memset(buff,0,sizeof(buff));
        ssize_t n = read(fd,buff,sizeof(buff));
        //若期望写入sizeof(buff)则最后会出现乱码！！！
        if(n > 0)   write(copy_file,buff,n);//注意：这里期望写入的字符个数是读取的字符个数！！！
        else if(n == 0)
        {
            printf("client quit....\n");
            break;
        }
        else
        {
            perror("read:");
            break;
        }
    }
    close(fd);
    close(copy_file);
    return 0;
}